#!/bin/sh
### BEGIN INIT INFO
# Provides:          noVNCd
# Required-Start:    $syslog $network $gitea
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: noVNC server daemon.
# Description:       noVNC server daemon.
### END INIT INFO

# Author: Cracy3m

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
DESC="noVNC server"

NAME=noVNCd
PIDFILE=/run/$NAME   #.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON=/opt/noVNC/utils/launch.sh

VNCHostName=localhost
VNCPort=5900
ListenPort=6080

VNCDisplay="1"    #空隔分开 表示开启多个VNC代理显示连接服务
VNCUserLabel=VNCUser_  #用户变量的前缀标识
VNCUser_1=yjf            #不同VNC代理显示连接服务 对应的登录帐户 这个例子是显示号为1 的帐户
DefUser=yjf

#USER=root

STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/1/KILL/5}"  #超时强制停止时间 格式 信号/超时秒数

VNCServerChkCMD="netstat -lp | grep -i vnc"


# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

#Exit if the $VNCDisplay var is empty  
if [ -z "${VNCDisplay}" ] ; then echo -e "env VNCDisplay is empty";fi
[ -n "${VNCDisplay}" ] || exit 0

do_start()
{
    #Exit if vncserver not running
    sleep 3
    VNCServerChk=$(eval $VNCServerChkCMD)
    if [ -z "$VNCServerChk" ];then echo -e "no vncserver";exit 0 ;fi
    
    for _display in $VNCDisplay
    do
        disp=$(( VNCPort + _display))
        DaemonArgs="--vnc ${VNCHostName}:${disp} --listen ${ListenPort}"
        user="${VNCUserLabel}${_display}"
        user="\$${user}"
        user=$(eval echo $user)
        if [ -z $user ];then user=$DefUser;fi;
        pidf="${PIDFILE}.${_display}.pid"
        start-stop-daemon --start --quiet --pidfile $pidf --make-pidfile --background --chuid $user --exec $DAEMON -- $DaemonArgs
    done 
    #sh -c "USER=$USER HOME=/home/$USER GITEA_WORK_DIR=$WORKINGDIR start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --chdir $WORKINGDIR --chuid $USER --exec $DAEMON -- $DAEMON_ARGS"
}

do_stop()
{
    for _display in $VNCDisplay
    do
        pidf="${PIDFILE}.${_display}.pid"
        start-stop-daemon --stop --quiet --pidfile $pidf  --oknodo --retry=$STOP_SCHEDULE  #--name $NAME
        rm -f $pidf
    done 
    #start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --oknodo --retry=$STOP_SCHEDULE
    #rm -f $PIDFILE
}

do_status()
{
    for _display in $VNCDisplay
    do
        pidf="${PIDFILE}.${_display}.pid"
        if [ -f $pidf ]; then
        if kill -0 $(cat "$pidf"); then
            echo "$NAME is running, PID is $(cat $pidf)"
        else
            echo "$NAME process is dead, but pidfile exists"
        fi
        else
            echo "$NAME is not running"
        fi
    done
    
}

case "$1" in
    start)
        echo "Starting $DESC" "$NAME"
        do_start
        ;;
    stop)
        echo "Stopping $DESC" "$NAME"
        do_stop
        ;;
    status)
        do_status
        ;;
    restart)
        echo "Restarting $DESC" "$NAME"
        do_stop
        do_start
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 2
        ;;
esac

exit 0
